package com.fh.fruit.service.activity.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.fruit.entity.activity.Activity;
import com.fh.fruit.service.activity.ActivityManager;
import com.fh.util.DateUtil;
import com.fh.util.PageData;

/**
 * 区域商品详情Service
 * 
 * @author Administrator
 * @version 2017.11.30 15:39
 */
@Service("activityService")
public class ActivityService implements ActivityManager {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/**
	 * 获取活动
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @return
	* @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Activity> getActivity(Page page) throws Exception {
		return (List<Activity>) dao.findForList("ActivityManager.getActivity",page);
	}

	/**
	  * 添加活动
	 * @author 葛欢欢
	 * @version 2017.11.30 16:40 
	 * @param activity
	 * @return
	 * @throws Exception 
	  */
	public int addActivity(PageData pd) throws Exception {
		Activity activity = new Activity();
		activity.setCode(pd.getString("code"));
		activity.setName(pd.getString("name"));
		activity.setRemark(pd.getString("remark"));
		activity.setLabel(pd.getString("label"));
		activity.setStartTime(pd.getString("startTime"));
		activity.setEndTime(pd.getString("endTime"));
		activity.setState(pd.getString("state"));
		activity.setType(pd.getString("type"));
		activity.setAction(pd.getString("action"));
		activity.setCreateId(pd.getString("createId"));
		activity.setCreateTime(pd.getString("createTime"));
		return (int) dao.save("ActivityManager.addActivity", activity);
	}

	/**
	  * 修改活动
	 * @author 葛欢欢
	 * @version 2017.11.30 16:40 
	 * @param activity
	 * @return
	 * @throws Exception
	  */
	public int updateActivity(PageData pd) throws Exception {
		Activity activity = new Activity();
		activity.setId(Integer.parseInt(pd.getString("id")));
		activity.setCode(pd.getString("code"));
		activity.setName(pd.getString("name"));
		activity.setRemark(pd.getString("remark"));
		activity.setLabel(pd.getString("label"));
		activity.setStartTime(pd.getString("startTime"));
		activity.setEndTime(pd.getString("endTime"));
		activity.setState(pd.getString("state"));
		activity.setType(pd.getString("type"));
		activity.setAction(pd.getString("action"));
		activity.setUpdateId(pd.getString("updateId"));
		activity.setUpdateTime(pd.getString("updateTime"));
		activity.setCreateId(pd.getString("createId"));
		activity.setCreateTime(pd.getString("createTime"));
		return (int) dao.update("ActivityManager.updateActivity", activity);
	}

	 /**
	  * 删除活动
	 * @author 葛欢欢
	 * @version 2017.11.30 16:40 
	 * @param id
	 * @return
	 * @throws Exception
	  */
	public int deleteActivity(int id) throws Exception {
		return (int) dao.delete("ActivityManager.deleteActivity", id);
	}

	@Override
	public Activity getActivityById(int id) throws Exception {
		return (Activity) dao.findForObject("ActivityManager.getActivityById", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getListActivity(Page page) throws Exception {
		PageData pd =page.getPd();
		String endTime=pd.getString("endTime");
		String kd=pd.getString("kd");
		if(kd ==null || "".equals(kd)){
			if(endTime != null && !"".equals(endTime)){
				pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
				page.setPd(pd);
			}
		}
		List<Map<String, Object>> maps=(List<Map<String, Object>>)dao.findForList("ActivityManager.getActivitylistPage", page);
		List<Map<String, Object>> section =(List<Map<String, Object>>) dao.findForList("ActivityManager.getGradeSection");
		if(maps != null && maps.size()>0) {
			for (Map<String, Object> map : maps) {
				BigDecimal add_rebate=(BigDecimal) map.get("add_rebate");
				BigDecimal add_rebate_dream=(BigDecimal) map.get("add_rebate_dream");
				BigDecimal income=add_rebate.add(add_rebate_dream);
				map.put("income", income.setScale(2,BigDecimal.ROUND_DOWN));
				BigDecimal add_consume=(BigDecimal) map.get("add_consume");
				BigDecimal cur_add_repeat=(BigDecimal) map.get("cur_add_repeat");
				BigDecimal rebate=add_consume.add(cur_add_repeat);
				if(section != null && section.size()>0){
					for(int i=0;i<section.size();i++){
						if(rebate.compareTo(new BigDecimal((String)section.get(i).get("value"))) < 0) {
							if(i==0){
								map.put("grade", 0);
							}else{
								map.put("grade", i+1);
							}
							break;
						}
						if(rebate.compareTo(new BigDecimal((String) section.get(section.size()-1).get("value"))) >= 0) {
							map.put("grade", section.size());
							break;
						}
						if(i==section.size()-1){
							map.put("grade", 0);
							break;
						}
						if(rebate.compareTo(new BigDecimal((String) section.get(i).get("value"))) >= 0 && rebate.compareTo(new BigDecimal((String) section.get(i+1).get("value"))) < 0) {
							map.put("grade", i+1);
							break;
						}
					}
				}
			}
		}
		return maps;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getGradeSection() throws Exception {
		return (List<Map<String, Object>>) dao.findForList("ActivityManager.getGradeSection");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMultGoods(Page page) throws Exception {
		PageData pd =page.getPd();
		String endTime=pd.getString("endTime");
		String kd=pd.getString("kd");
		if(kd ==null || "".equals(kd)){
			if(endTime != null && !"".equals(endTime)){
				pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
				page.setPd(pd);
			}
		}
		List<Map<String, Object>> maps= (List<Map<String, Object>>) dao.findForList("ActivityManager.getMultGoodslistPage", page);
		for (Map<String, Object> map : maps) {
			BigDecimal real_price=(BigDecimal)map.get("real_price");
			if(real_price !=null){
				map.put("real_price", real_price.setScale(2, BigDecimal.ROUND_DOWN));
			}
		}
		return maps;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMultGoods(PageData pd) throws Exception {
		String endTime=pd.getString("endTime");
		String kd=pd.getString("kd");
		if(kd ==null || "".equals(kd)){
			if(endTime != null && !"".equals(endTime)){
				pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
			}
		}
		return (List<Map<String, Object>>) dao.findForList("ActivityManager.getMultGoodslist", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getMultCount(PageData pd) throws Exception {
		return (Map<String, Object>) dao.findForObject("ActivityManager.getMultCount", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMultGoodsCount(PageData pd) throws Exception {
		List<Map<String, Object>> goodsList=new ArrayList<Map<String,Object>>();
		Map<String,Object> m= new HashMap<>();
	    goodsList=(List<Map<String, Object>>) dao.findForList("ActivityManager.getMultGoodsCount", pd);
	    for (Map<String, Object> map : goodsList) {
			 map.put("startTime", pd.getString("startTime"));
			 map.put("endTime", pd.getString("endTime"));
			m=(Map<String,Object>) dao.findForObject("ActivityManager.getMultGoodsCount1", map);
			if(m!= null){
				BigDecimal bd=(BigDecimal)m.get("price");
				map.put("num", m.get("num"));
				map.put("price", bd.setScale(2,BigDecimal.ROUND_HALF_UP));
			}else{
				map.put("num", 0);
				map.put("price", 0);
			}
		}
	    return goodsList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getUserDetails(PageData pd) throws Exception {
		return (Map<String, Object>) dao.findForObject("ActivityManager.getUserDetails", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getBonusList(Page page) throws Exception {
		return (List<Map<String, Object>>) dao.findForList("ActivityManager.getBonuslistPage", page);
	}

}
